﻿using System.Drawing;

namespace QAQ
{
    class IM
    {
        public struct ImgFeature 
        {
            public int V;
            public int L;
            public int R;
            public Point P;
      
            public ImgFeature(int V,int L,int R, int X,int Y)
            {
                this.V = V;
                this.L = L;
                this.R = R;
                this.P = new Point(X, Y);
            }
        }
        private int[] ValueNode ={9,10,11,12,13,
                              14,15,16,17,18};
        private ImgFeature[] mImgFeature = new ImgFeature[19];

        public IM() 
        {

            //node list
            mImgFeature[0] = new ImgFeature(-1, 1, 5, 0, 3);
            mImgFeature[1] = new ImgFeature(-1, 2, 3, 0, 1);
            mImgFeature[2] = new ImgFeature(-1, ValueNode[4], ValueNode[7], 0, 0);
            mImgFeature[3] = new ImgFeature(-1, ValueNode[1], 4, 0, 2);
            mImgFeature[4] = new ImgFeature(-1, ValueNode[2], ValueNode[3], 1, 4);
            mImgFeature[5] = new ImgFeature(-1, 6, 7, 3, 3);
            mImgFeature[6] = new ImgFeature(-1, ValueNode[6], ValueNode[5], 0, 0);
            mImgFeature[7] = new ImgFeature(-1, 8, ValueNode[0], 0, 4);
            mImgFeature[8] = new ImgFeature(-1, ValueNode[9], ValueNode[8], 0, 5);
            //value list
            for(int i=0;i<10;++i){
                mImgFeature[ValueNode[i]] = new ImgFeature(i, 0, 0, -1, -1);
            }
        }
        public int getNum(Bitmap src){
            int it=0;

            while (mImgFeature[it].V == -1)
            {
                if (src.GetPixel(mImgFeature[it].P.X, mImgFeature[it].P.Y).ToArgb() == Color.White.ToArgb())
                {
                    it = mImgFeature[it].R;
                }
                else
                {
                    it = mImgFeature[it].L;
                }/*
                if (it == -1)
                {
                    return -1; //err!
                }*/
            }
            return mImgFeature[it].V;
        }
        public void Dispose()
        {
            ValueNode = null;
            mImgFeature = null;            
        }
    }
}
